<?php
// +----------------------------------------------------------------------
// | Links(友情链接
// +----------------------------------------------------------------------
// | @link ( http://www.yurnero.net )
// +----------------------------------------------------------------------
// | @copyright
// +----------------------------------------------------------------------
// | @licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | @author Haijun Wu <nicholasinlove@126.com>
// +----------------------------------------------------------------------
// | $Id: LinksWidget.class.php 119 2011-05-13 08:05:33Z nicholasinlove1986@gmail.com $
// +----------------------------------------------------------------------


class LinksWidget extends Widget {

	/**
	 * 构造友情链接
	 * 
	 */
	public function render($data) {
	
		$link  =  f_static_cache('links_static','',ROOT_PATH . STATIC_CACHES_DIR);
		
	    $output = '';
		$output .= '<!-- Links: START -->'."\n";
		$output .= " <li class=\"widget widget_links dbx-box\">\n";
		$output .= "  <h3 class=\"dbx-handle\">Links</h3>\n";
		$output .= "   <ul class='dbx-content'>\n";
		$output .= $this->createLinks($link);
		$output .= "   </ul>\n";
		$output .= " </li>\n";
		$output .= ' <!-- Links: END -->'."\n";
		
		return $output;
	}
	
	public function createLinks($link) {
	
		$ss    =  f_static_cache('statistics_static','',ROOT_PATH . STATIC_CACHES_DIR);
		$hasMore = ($ss['link_num'] <= C('links_num')) ? 0 : C('links_num');
		
		$result = '';
		$count = 0;
		if ( $link ) {
			foreach ($link as $v) {
				if ( C('links_columns') == 1 ) {
					$result .= '<li class="ml_item"><a href="' .$v['link_url'].'" rel="external" title="' .$v['link_description'].'"   style="background:url(http://www.google.com/s2/favicons?domain='.$this->replaceHttp($v['link_url']).') no-repeat;">' .$v['link_name'].'</a></li>';
				} else {
					if ( $count % C('links_columns') == 0 ) {
						$result .= '<li class="ml_item"><div>';
					}
					$result .= '<div class="ml_col ml_col_' . C('links_columns') . '"><a href="' .$v['link_url'].'" rel="external" title="' .$v['link_description'].'" style="background:url(http://www.google.com/s2/favicons?domain='.$this->replaceHttp($v['link_url']).') no-repeat;">' .$v['link_name'].'</a></div>';
					++$count;
					if ( $count % C('links_columns') == 0 ) {
						$result .= '<div class="ml_fixed"></div></div></li>';
					}
				}
			}
			if ( $count % C('links_columns') != 0 ) {
				$result .= '<div class="ml_fixed"></div></div></li>';
			}
			if ( $hasMore ) {
				$showAll = '<a class="ml_showall" href="javascript:void(0);" onclick="RCJS.showall(\'__APP__/Public/allLinks\',\'Loading\');">Show all &raquo;</a>';
				$result .= '<li class="ml_nav"><div>' . $showAll . '<div class="ml_fixed"></div></div></li>';
			}
			return $result;	
		}
	}
	
	protected function replaceHttp($str) {
		if (strpos($str, 'http://') === false && strpos($str, 'https://') === false) {
			return "";
		} else {
		    return preg_replace("/https?:\/\/|\//","",$str);
		}	
	}
	
}

?>
